package com.cerosoft;

import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;

import com.cerosoft.api.Config;
import com.cerosoft.components.CustomedTitleBar;
import com.cerosoft.components.DynamicVideoView;
import com.cerosoft.components.PushButton;
import com.cerosoft.utils.EventManager.EventType;

public class VideoHandlerActivity extends AbstractHandlerActivity {
	
	private DynamicVideoView vv = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.videohandlerview);

		Intent intent = getIntent();
		String ipAddr = intent.getStringExtra(Config.IP_ADDR);
		String path = intent.getStringExtra(Config.PATH);
		String[] paths = intent.getStringArrayExtra(Config.MEDIAS);
		List<String> pathList = Arrays.asList(paths);
		int index = pathList.indexOf(path);

		CustomedTitleBar bar = (CustomedTitleBar) findViewById(R.id.result_txt);
		bar.initTitleBar(pathList, index);
		getMyMgr().addListener(bar);

		vv = (DynamicVideoView) findViewById(R.id.videohandler);
		vv.setMediaController(new MediaController(this));
		vv.initView(pathList, ipAddr, index);
		getMyMgr().addListener(vv);

		PushButton prev = (PushButton) findViewById(R.id.prev_btn);
		prev.setOnClickListener(this);
		prev.initBtn(EventType.Prev, index, pathList.size());
		getMyMgr().addListener(prev);

		PushButton next = (PushButton) findViewById(R.id.next_btn);
		next.setOnClickListener(this);
		next.initBtn(EventType.Next, index, pathList.size());
		getMyMgr().addListener(next);

		ImageView btn = (ImageView) findViewById(R.id.backbtn);
		btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.backbtn: {
			vv.stopPlayback();
			finish();
			break;
		}
		case R.id.prev_btn: {
			getMyMgr().fireEvent(EventType.Prev);
			break;
		}
		case R.id.next_btn: {
			getMyMgr().fireEvent(EventType.Next);
			break;
		}
		}
	}
}
